package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.children.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChildDaoImpl extends TempDao {

    public Map<Integer, ChildTalentInfo> getChildTalentInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChildTalentInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_child_talent";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChildTalentInfo data = new ChildTalentInfo();
                data.setId(rs.getInt("id"));
                data.setTotalGrowth(rs.getInt("growth"));
                data.setTrainAddGrowth(rs.getInt("trainingGrowth"));
                data.setTrainAddPatronsExpParam(rs.getInt("trainingExp"));
                data.setEarnSpeedAddition(rs.getInt("earnSpeedAddition"));
                data.setAddItemCount(rs.getInt("rewardItemNum"));
                String grandsonQualityStr = rs.getString("grandsonQuality");
                List<Integer> list = new ArrayList<>();
                for (String s : grandsonQualityStr.split("\\|")) {
                    list.add(Integer.parseInt(s));
                }
                data.setGrandsonQuality(list);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public void getChildOccupationMap(Map<Integer, ChildOccupation> childOccupationMap, Map<Integer, List<ChildOccupation>> childOccupationTypeMap){
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_child_occupation";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChildOccupation data = new ChildOccupation();
                data.setOccupationId(rs.getInt("id"));
                data.setOccupationType(rs.getInt("type"));
                data.setMinParam(rs.getInt("minRng"));
                data.setMaxParam(rs.getInt("maxRng"));
                data.setSexLimit(rs.getInt("sexLimit"));
                data.setWeight(rs.getInt("weight"));
                data.setLonely(rs.getBoolean("canSingle"));
                childOccupationMap.put(data.getOccupationId(), data);
                if(!childOccupationTypeMap.containsKey(data.getOccupationType())){
                    childOccupationTypeMap.put(data.getOccupationType(), new ArrayList<>());
                }
                childOccupationTypeMap.get(data.getOccupationType()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public Map<Integer, ChildTrainInfo> getChildTrainInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChildTrainInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_child_train";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChildTrainInfo data = new ChildTrainInfo();
                data.setTrainType(rs.getInt("id"));
                data.setNeedTitle(rs.getInt("unlock"));
                data.setEarnSpeedAddition(rs.getInt("earnSpeedAddition"));
                List<Integer> unlockSiteCostList = new ArrayList<>();
                unlockSiteCostList.add(rs.getInt("pitNum1"));
                unlockSiteCostList.add(rs.getInt("pitNum2"));
                unlockSiteCostList.add(rs.getInt("pitNum3"));
                unlockSiteCostList.add(rs.getInt("pitNum4"));
                unlockSiteCostList.add(rs.getInt("pitNum5"));
                data.setUnlockSiteCostList(unlockSiteCostList);

                dataMap.put(data.getTrainType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<ChildNameData> getChildNameDataList() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_child_name";
        List<ChildNameData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChildNameData data = new ChildNameData();
                data.setMenName(rs.getString("nameMen"));
                data.setWomenName(rs.getString("nameWomen"));
                data.setLanguage(rs.getString("language"));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public List<ChildAppearance> getChildAppearanceList() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_child_appearance";
        List<ChildAppearance> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChildAppearance data = new ChildAppearance();
                data.setId(rs.getInt("id"));
                data.setOccupation(rs.getInt("occupation"));
                data.setSex(rs.getInt("sex"));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }
}
